package com.wy.xinzhi.common.actions;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.wy.component.properties.PropertiesUtil;
import com.wy.framework.actions.AbstractBaseAction;
import com.wy.framework.exceptions.ServiceException;
import com.wy.xinzhi.common.constant.Constants;
import com.wy.xinzhi.common.pojos.TCustomerCheck;
import com.wy.xinzhi.common.services.CustomerCheckService;
import com.wy.xinzhi.common.utils.DateUtil;

/**
 * 健身预约
 * 
 * @author X
 * 
 */
@ParentPackage("json")
@Namespace(value = "/xz")
@SuppressWarnings({ "rawtypes", "unused" })
public class CustomerCheckAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;
	private static Log logger = LogFactory.getLog(CustomerCheckAction.class);
	private PropertiesUtil p = new PropertiesUtil();

	private String jsonRes;
	// 每日预约时间段
	private List<Map<String, Object>> reservePeriods;
	// 用户预约记录
	private List<Map<String, Object>> customerChecks;
	// 是否空闲
	private String isIdle;

	@Autowired
	private CustomerCheckService customerCheckService;

	/**
	 * 获取每天的预约时间段信息
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@Action(value = "queryReservePeriod", results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String queryReservePeriod() throws UnsupportedEncodingException {
		String checkDate = ServletActionContext.getRequest().getParameter(
				"checkDate");
		String storeId = ServletActionContext.getRequest().getParameter(
				"storeId");
		reservePeriods = customerCheckService.queryReservePeriods(checkDate,
				storeId);
		if (reservePeriods != null) {
			this.jsonRes = "true";
		} else {
			this.jsonRes = "false";
		}
		return SUCCESS;
	}

	/**
	 * 获取预约信息列表
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@Action(value = "queryCheckInfo", results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String queryCheckInfo() throws UnsupportedEncodingException {
		String customerId = ServletActionContext.getRequest().getParameter(
				"customerId");
		customerChecks = customerCheckService.queryCustomerChecks(customerId);
		if (reservePeriods != null) {
			this.jsonRes = "true";
		} else {
			this.jsonRes = "false";
		}
		return SUCCESS;
	}

	/**
	 * 预约时间
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws ServiceException
	 */
	@Action(value = "addCustomCheck", results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String addCustomCheck() throws UnsupportedEncodingException,
			ServiceException {
		String customerId = ServletActionContext.getRequest().getParameter(
				"customerId");
		String checkDate = ServletActionContext.getRequest().getParameter(
				"checkDate");
		String periodIds = ServletActionContext.getRequest().getParameter(
				"periodIdS");
		String storeId = ServletActionContext.getRequest().getParameter(
				"storeId");
		Timestamp d = new Timestamp(System.currentTimeMillis());
		if (periodIds.indexOf(";") > -1) {
			String[] period = periodIds.split(";");
			for (String periodStr : period) {
				TCustomerCheck customerCheck = new TCustomerCheck();
				customerCheck.setCustomerId(customerId);
				customerCheck.setCheckDate(DateUtil.stringToDate(checkDate,
						Constants.DateConstants.FOMATE_YYYYMMDD));
				customerCheck.setPeriodId(Integer.parseInt(periodStr));

				customerCheck.setStoreId(Integer.parseInt(storeId));
				customerCheck.setCreateDate(d);
				customerCheck
						.setCheckStatus(Constants.CustomerCheck.CheckStatic.STATIC_APPL);
				customerCheckService.addCustomerCheck(customerCheck);
			}
		} else {
			TCustomerCheck customerCheck = new TCustomerCheck();
			customerCheck.setCustomerId(customerId);
			customerCheck.setCheckDate(DateUtil.stringToDate(checkDate,
					Constants.DateConstants.FOMATE_YYYYMMDD));
			customerCheck.setPeriodId(Integer.parseInt(periodIds));

			customerCheck.setStoreId(Integer.parseInt(storeId));
			customerCheck.setCreateDate(d);
			customerCheck
					.setCheckStatus(Constants.CustomerCheck.CheckStatic.STATIC_APPL);
			customerCheckService.addCustomerCheck(customerCheck);
		}

		return SUCCESS;
	}

	/**
	 * 删除预约记录
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws ServiceException
	 */
	@Action(value = "deleteCustomCheck", results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String deleteCustomCheck() throws UnsupportedEncodingException,
			ServiceException {
		String reId = ServletActionContext.getRequest().getParameter("reId");
		TCustomerCheck obj = new TCustomerCheck();
		obj.setReId(Integer.parseInt(reId));
		customerCheckService.deleteCustomerCheck(obj);
		return SUCCESS;
	}

	/**
	 * 修改预约记录
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws ServiceException 
	 */
	@Action(value = "updateCustomCheck", results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String updateCustomCheck() throws UnsupportedEncodingException, ServiceException {
		String reId = ServletActionContext.getRequest().getParameter("reId");
		String customerId = ServletActionContext.getRequest().getParameter(
				"customerId");
		String checkDate = ServletActionContext.getRequest().getParameter(
				"checkDate");
		String periodId = ServletActionContext.getRequest().getParameter(
				"periodId");
		String storeId = ServletActionContext.getRequest().getParameter(
				"storeId");
		String checkStatus = ServletActionContext.getRequest().getParameter(
				"checkStatus");
		TCustomerCheck obj = new TCustomerCheck();
		obj.setReId(Integer.parseInt(reId));
		obj.setCustomerId(customerId);
		obj.setCheckDate(DateUtil.stringToDate(checkDate,
				Constants.DateConstants.FOMATE_YYYYMMDD));
		obj.setPeriodId(Integer.parseInt(periodId));
		obj.setStoreId(Integer.parseInt(storeId));
		obj.setCheckStatus(checkStatus);
		obj.setCreateDate(new Timestamp(new Date().getTime()));
		// 需要确定获取用户信息后修改
		obj.setUserId(1);
		customerCheckService.updateCustomerCheck(obj);
		return SUCCESS;
	}

	/**
	 * 校验预约时间是否空闲
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@Action(value = "queryIsIdle", results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String queryIsIdle() throws UnsupportedEncodingException {
		String checkDate = ServletActionContext.getRequest().getParameter(
				"checkDate");
		String periodId = ServletActionContext.getRequest().getParameter(
				"periodId");
		String storeId = ServletActionContext.getRequest().getParameter(
				"storeId");
		boolean b = customerCheckService.getIsIdle(checkDate, periodId, storeId);
		if(b){
			isIdle = "true";
		}else{
			isIdle = "false";
		}
		return SUCCESS;
	}

	public String getJsonRes() {
		return jsonRes;
	}

	public void setJsonRes(String jsonRes) {
		this.jsonRes = jsonRes;
	}

	public List<Map<String, Object>> getReservePeriods() {
		return reservePeriods;
	}

	public void setReservePeriods(List<Map<String, Object>> reservePeriods) {
		this.reservePeriods = reservePeriods;
	}

	public List<Map<String, Object>> getCustomerChecks() {
		return customerChecks;
	}

	public void setCustomerChecks(List<Map<String, Object>> customerChecks) {
		this.customerChecks = customerChecks;
	}

	public void setIsIdle(String isIdle) {
		this.isIdle = isIdle;
	}

	public String getIsIdle() {
		return isIdle;
	}

	
}
